package org.mitoma.logger.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class RegisterPanel extends Composite {

  private static RegisterPanelUiBinder uiBinder = GWT
                                                    .create(RegisterPanelUiBinder.class);

  interface RegisterPanelUiBinder extends UiBinder<Widget, RegisterPanel> {
  }

  @UiField
  TextBox         email;
  @UiField
  PasswordTextBox password;
  @UiField
  Button          register;

  public RegisterPanel() {
    initWidget(uiBinder.createAndBindUi(this));
  }

  @UiHandler("register")
  public void register(ClickEvent event) {
    GlobalRepository.getService().preRegister(email.getText(),
        password.getText(), new AsyncCallback<Void>() {

          @Override
          public void onSuccess(Void result) {
            GlobalRepository.getMainPanel().content.clear();
            GlobalRepository.getMainPanel().content
                .add(new PreRegisterCompletePanel());
          }

          @Override
          public void onFailure(Throwable caught) {
            PopupPanel pp = new DecoratedPopupPanel(true, true);
            pp.setGlassEnabled(true);
            pp.add(new Label("ユーザ登録に失敗しました。"));
            pp.showRelativeTo(register);
          }
        });
  }
}
